package site.shihuan.helpdesk.service.manager;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.common.util.IdWorker;
import site.shihuan.helpdesk.service.dao.SvcCustomerDao;
import site.shihuan.helpdesk.service.dao.SvcFollowCustomerDao;
import site.shihuan.helpdesk.service.dao.SvcVisitorDao;
import site.shihuan.helpdesk.service.model.po.SvcCustomerPO;
import site.shihuan.helpdesk.service.model.po.SvcFollowCustomerPO;

import java.util.List;

/**
 * 联系人管理
 *
 * @author 周世焕
 * @Date 2021/2/20 11:52
 */
@Component
@Slf4j
public class CustomerManager {

    @Autowired
    private SvcCustomerDao customerDao;

    @Autowired
    private SvcVisitorDao visitorDao;

    @Autowired
    private SvcFollowCustomerDao followCustomerDao;

    /**
     * 查询客户列表
     * @param tenantId
     * @param userId
     * @return
     */
    public List<SvcCustomerPO> findCustomerList(String tenantId, String userId) {
        return customerDao.selectByTenantId(tenantId, userId);
    }

    /**
     * 查询客户
     * @param customerId
     * @return
     */
    public SvcCustomerPO findById(String customerId) {
        return customerDao.selectByPrimaryKey(customerId);
    }

    /**
     * 查询客户关注列表
     * @param tenantId
     * @param userId
     * @return
     */
    public List<SvcCustomerPO> findCustomerFollowList(String tenantId, String userId) {
        return customerDao.selectFollowByTenantId(tenantId, userId);
    }

    /**
     * 删除客户
     * @param tenantId
     * @param customerId
     * @return
     */
    public void deleteCustomer(String tenantId, String customerId) {
        customerDao.deleteByTenantId(tenantId, customerId);
    }

    @Transactional(rollbackFor = Exception.class)
    public SvcCustomerPO saveCustomer(SvcCustomerPO customerPO) {
        if(customerPO.getCustomerId()!=null) {
            customerDao.updateByPrimaryKey(customerPO);
        }else {
            customerPO.setCustomerId(IdWorker.nextId());
            customerDao.insertSelective(customerPO);
        }
        visitorDao.updateCustomerInfo(customerPO.getTenantId(),customerPO.getEmail(),
                customerPO.getCustomerId(), customerPO.getMobile());
        return customerPO;
    }

    /**
     * 关注客户
     * @param customerId
     * @param userId
     * @param tenantId
     */
    public void starCustomer(String customerId, String userId, String tenantId) {
        SvcFollowCustomerPO customerPO = new SvcFollowCustomerPO();
        customerPO.setCustomerId(customerId);
        customerPO.setUserId(userId);
        customerPO.setTenantId(tenantId);
        followCustomerDao.insertSelective(customerPO);
    }

    /**
     * 取消关注客户
     * @param customerId
     * @param userId
     * @param tenantId
     */
    public void unStarCustomer(String customerId, String userId, String tenantId) {
        followCustomerDao.deleteByPrimaryKey(userId, customerId, tenantId);
    }
}
